// pages/addressBook/addressBook.js

const app = getApp()
const api = require('../../utils/util')
Page({
    // 页面分享
    onShareAppMessage () {},
    // 分享朋友圈
    onShareTimeline () {},

    /**
     * 页面的初始数据
     */
    data: {
        imagesUrl: app.globalData.siteUrl,
        // 搜素
        value: '',
        address: {
          
        },
        indexList:[],
        //判断登录状态
        isLogin:false,
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

      let userinfo = app.globalData.userinfo;
      console.log(userinfo)
      if(!userinfo){
        wx.redirectTo({
          url: '/pages/login/login',
        })
      }

      let that = this
      let param = {
        str: ''
      }
      api.address_index(param).then(function(res){
          let indexListKey =  Object.keys(res.data)
          that.setData({
             indexList:indexListKey,
             address: res.data
          })
      })

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {
      wx.showLoading();
      setTimeout(function () {
             wx.hideLoading();
             wx.stopPullDownRefresh();
      }, 2000);
      
    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {
      wx.showLoading();
      setTimeout(function () {
             wx.hideLoading();
             wx.stopPullDownRefresh();
      }, 2000);
    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    },
    // 通用输入函数
    onChange: function(e){
      this.setData({
        value: e.detail
      })
      if(e.detail == ''){
        let that = this
      let param = {
        str: ''
      }
      api.address_index(param).then(function(res){
          let indexListKey =  Object.keys(res.data)
          that.setData({
             indexList:indexListKey,
             address: res.data
          })
      })
      }
    },
    onSearch: function(){
     
      let userinfo = app.globalData.userinfo;
      console.log(userinfo)
      if(!userinfo){
        wx.navigateTo({
          url: '/pages/login/login',
        })
      }
        
       let that = this
      
        let param = {
          str: that.data.value  
        }
        api.address_index(param).then(function(res){
          let indexListKey =  Object.keys(res.data)
          that.setData({
             indexList:indexListKey,
             address: res.data
          })
      })
    },
    toLogin: function(){
      wx.redirectTo({
        url: '/pages/login/login',
      })
   },
})